import { Notification } from 'element-ui'
import request from './request'

function Uploader(
  config = config || {
    params: null,
    size: null,
    action: null,
    accept: null,
    upload: null,
    beforeUpload: null,
    execute: null,
    uploadSuccess: null,
    checkFileSuffix: null
  }
) {
  this.action = config.action
  this.size = config.size
  this.accept = config.accept

  this.upload =
    config.upload ||
    function (file, data) {
      this.file = file
      this.data = data
      this.before().then(() => {
        this.execute(file, data)
      })
    }

  this.execute =
    config.execute ||
    function (file, data) {
      var form = new FormData()
      form.append('file', this.file)
      var data = this.data
      if (data) {
        for (var i in data) {
          form.append(i, data[i])
        }
      }
      if (!this.action) {
        Notification({
          title: '系统消息',
          type: 'success',
          message: '请设置文件上传路径'
        })
      }
      request({
        url: this.action,
        data: form,
        method: 'put',
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        this.uploadSuccess(res)
      })
    }

  /**
   * 文件上传成功回调
   */
  this.uploadSuccess = config.uploadSuccess || function (res) {}

  /**
   * 上传前处理
   */
  this.before =
    config.beforeUpload ||
    function () {
      this.checkFileExists()
      var file = this.file
      return new Promise((resolve, reject) => {
        var suffixFlag = this.checkFileSuffix(file)
        var sizeFlag = this.checkFileSize(file)
        if (suffixFlag && sizeFlag) {
          resolve()
        } else {
          reject()
        }
      })
    }

  /**
   * 检查文件大小
   */
  this.checkFileSize =
    config.checkFileSize ||
    function () {
      this.checkFileExists()
      var file = this.file
      if (!this.size) {
        return true
      }
      if (file.size > this.size) {
        Notification({
          title: '系统消息',
          message: '文件不可超过' + this.getFileSize(this.size) + ',请处理一下哦'
        })
        return false
      } else {
        return true
      }
    }

  /**
   * 检查文件后缀
   */
  this.checkFileSuffix =
    config.checkFileSuffix ||
    function () {
      this.checkFileExists()
      var file = this.file
      if (this.accept === '*' || !this.accept) {
        return true
      }
      var suffixes = this.accept.split(',')
      var fileSuffix = file.name.substring(file.name.lastIndexOf('.'))
      if (suffixes.indexOf(fileSuffix) === -1) {
        Notification({
          title: '系统消息',
          message: '请上传格式为' + this.accept + '的文件哦'
        })
        return false
      } else {
        return true
      }
    }

  /**
   * 检查文件是否存在
   * @returns
   */
  this.checkFileExists = function () {
    if (!this.file) {
      Notification({
        message: '请选择要上传的文件',
        type: 'error',
        duration: 5 * 1000
      })
      return
    }
  }
}

/**
 * 获取文件大小
 */
Uploader.prototype.getFileSize = function (fileByte) {
  var fileSizeByte = fileByte
  var fileSizeMsg = ''
  if (fileSizeByte < 1048576) {
    fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + 'KB'
  } else if (fileSizeByte == 1048576) fileSizeMsg = '1MB'
  else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) {
    fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + 'MB'
  } else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) {
    fileSizeMsg = '1GB'
  } else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) {
    fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
  } else fileSizeMsg = '1TB'
  return fileSizeMsg
}

export default Uploader
